package com.ruoyi.controller.basic;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.basic.MesBom;
import com.ruoyi.service.basic.IMesBomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BOM主表Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/mes/bom")
public class MesBomController extends BaseController {
    @Autowired
    private IMesBomService mesBomService;

    /**
     * 查询BOM主表列表
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesBom mesBom) {
        startPage();
        List<MesBom> list = mesBomService.selectMesBomList(mesBom);
        return getDataTable(list);
    }

    /**
     * 导出BOM主表列表
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:export')")
    @Log(title = "BOM主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesBom mesBom) {
        List<MesBom> list = mesBomService.selectMesBomList(mesBom);
        ExcelUtil<MesBom> util = new ExcelUtil<MesBom>(MesBom.class);
        util.exportExcel(response, list, "BOM主表数据");
    }

    /**
     * 获取BOM主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:query')")
    @GetMapping(value = "/{bomId}")
    public AjaxResult getInfo(@PathVariable("bomId") Long bomId) {
        return success(mesBomService.selectMesBomByBomId(bomId));
    }

    /**
     * 新增BOM主表
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:add')")
    @Log(title = "BOM主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesBom mesBom) {
        // 验证必填字段
        if (mesBom.getProductId() == null) {
            return error("产品ID不能为空");
        }
        return toAjax(mesBomService.insertMesBom(mesBom));
    }

    /**
     * 修改BOM主表
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:edit')")
    @Log(title = "BOM主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesBom mesBom) {
        return toAjax(mesBomService.updateMesBom(mesBom));
    }

    /**
     * 删除BOM主表
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:remove')")
    @Log(title = "BOM主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bomIds}")
    public AjaxResult remove(@PathVariable Long[] bomIds) {
        return toAjax(mesBomService.deleteMesBomByBomIds(bomIds));
    }

    /**
     * 校验BOM编码
     */
    @PostMapping("/checkBomCodeUnique")
    public AjaxResult checkBomCodeUnique(@RequestBody MesBom mesBom) {
        String result = mesBomService.checkBomCodeUnique(mesBom);
        if ("0".equals(result)) {
            return success();
        }
        return error("BOM编码已存在");
    }

    /**
     * 根据产品编码查询BOM
     */
    @GetMapping("/getByProductCode/{productCode}")
    public AjaxResult getByProductCode(@PathVariable String productCode) {
        List<MesBom> list = mesBomService.selectMesBomByProductCode(productCode);
        return success(list);
    }
}